<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%request.setCharacterEncoding("utf-8"); %>

<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>角色管理</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <!--bootstrap.min.css-->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <!--font-awesome.css?v=4.4.0-->
    <link href="https://cdn.staticfile.org/font-awesome/4.4.0/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <!--bootstrap.min.css-->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <!--custom.css-->
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/plugins/iCheck/custom.css"/>
	<!--style.css-->
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/style.css"/>
	<!--jquery.min.js-->
	<script src="https://cdn.staticfile.org/jquery/2.1.4/jquery.min.js"></script>

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <form action="<%=basePath%>BackstageRoleServlet/listAdminInfo" method="post">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>角色管理</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-3 pull-right">
                            <div class="input-group">
                                <input type="text" placeholder="请输入角色名字..." value="${admin_name}" name="admin_name" class="form-control">
                                <span class="input-group-btn">
                                <button type="submit" class="btn btn-sm btn-primary" style="width: 70px;height: 34px;text-align: center;line-height: 23px;">&nbsp;搜索</button> </span>
                            </div>
                        </div>
                        <!-- 四个按钮 -->
                    <div class="btn btn-primary fa fa-plus" onclick="addAdmin()" style="margin-left:15px;border-radius: 0%;width: 80px;height: 40px;text-align: center;line-height: 27px;">&nbsp;新增</div>
                    <div class="btn btn-primary fa fa-edit" onclick="editAdmin()" style="border-radius: 0%;width: 80px;height: 40px;text-align: center;line-height: 27px;">&nbsp;修改</div>
                    <div class="btn btn-primary fa fa-recycle" onclick="deleteAdmin()" style="border-radius: 0%;width: 80px;height: 40px;text-align: center;line-height: 27px;">&nbsp;删除</div>
                    <div class="btn btn-primary fa fa-refresh" onclick="resetAdminPass()" style="border-radius: 0%;width: 180px;height: 40px;text-align: center;line-height: 27px;">&nbsp;初始化密码123456</div>
                    </div>
                    
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                            <tr>
                                <th>选择</th>
                                <th>角色账号</th>
                                <th>角色</th>
                                <th>角色名</th>
                                <th>创建人</th>
                                <th>状态</th>
                                <th>最后修改时间</th>
                            </tr>
                            </thead>
                            <tbody>
                           <c:forEach items="${adminInfoList }" var="admin">
                            <tr admin_id="${admin.admin_id}">
                                <td>
                                    <input type="checkbox" class="i-checks aid" value="${admin.admin_id}" admin_id="${admin.admin_id}" name="admin_id">
                                </td>
                                <td>${admin.admin_account}</td>
                                <td>${admin.role_name}</td>
                                <td>${admin.admin_name}</td>
                                <td>${admin.admin_create}</td>
                                <c:if test="${admin.state_name=='启用'}">
                                	<td align="center" style="line-height: 34px;"><input type="button" onclick="transitionState(this)" admin_id="${admin.admin_id}" state_id="${admin.state_id}" style="margin-left:-60px" class="btn btn-info btn-xs" value="${admin.state_name}" /></td>
                                </c:if>
                               <c:if test="${admin.state_name=='禁用'}">
                                	<td align="center" style="line-height: 34px;"><input type="button" onclick="transitionState(this)" admin_id="${admin.admin_id}" state_id="${admin.state_id}" style="margin-left:-60px" class="btn btn-danger btn-xs" value="${admin.state_name}" /></td>
                               </c:if>
                                <td id="${admin.admin_time}">${admin.admin_time }</td>
                            </tr>
                           </c:forEach>
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>

    </div>
    </form>
    <input type="hidden" id="panduan" value="10" />
    <div class="pag text-center"></div>
</div>
<!--bootstrap.min.js-->
<script src="https://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<!--jquery.peity.min.js-->
<script src="<%=basePath%>js/plugins/peity/jquery.peity.min.js" type="text/javascript" charset="utf-8"></script>
<!--content.js-->
<script src="<%=basePath%>js/content.js" type="text/javascript" charset="utf-8"></script>
<!--icheck.min.js-->
<script src="<%=basePath%>js/plugins/iCheck/icheck.min.js" type="text/javascript" charset="utf-8"></script>
<!--peity-demo.js-->
<script src="<%=basePath%>js/demo/peity-demo.js" type="text/javascript" charset="utf-8"></script>
<!--layer.min.js-->
<script src="<%=basePath%>js/plugins/layer/layer.min.js" type="text/javascript" charset="utf-8"></script>
<!-- toastr.min.js -->
<script src="<%=basePath%>js/plugins/toastr/toastr.min.js" type="text/javascript" charset="utf-8"></script>
<!-- jquery-bootstrap-pagination.js -->
<script src="<%=basePath%>js/plugins/jquery-bootstrap-pagination/jquery-bootstrap-pagination.js" type="text/javascript" charset="utf-8"></script>
<!-- pubic.js -->
<script src="<%=basePath%>js/pubic.js" type="text/javascript" charset="utf-8"></script>
<script>
    $(document).ready(function () {
        $(".i-checks").iCheck({checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green",})
    });
   
  	//提示信息
  	if('${param.result}'!=''){
  		layer.msg('${param.result}',{
  			time:1000
  		});
  	}
  	//初始化管理员密码
  	function resetAdminPass(){
  		if($(':checked').length==0){
  			layer.msg('至少选择一条记录！！！',{icon:5});
  		}else if($(':checked').length==1){
  			layer.msg('您确定要初始化吗?',{
  				time:4000,
  				btn: ['确定','取消'],
  				yes:function(index){
  					$.get('${pageContext.request.contextPath}/BackstageRoleServlet/resetAdminPass',{"admin_id":$('.aid:checked').attr('admin_id')},function(data){
  		  				if(data==1){
  		  					layer.msg('初始化成功',{
  		  						time:1000
  		  					});
  		  				}else{
  		  					layer.msg('初始化失败',{
  		  						time:1000
  		  					});
  		  				}
  		  			});
  				}
  			});
  		}else if($(':checked').length>1){
  			layer.msg('您确定要初始化吗?',{
  				time:4000,
  				btn: ['确定','取消'],
  				yes:function(index){
  					$.get('${pageContext.request.contextPath}/BackstageRoleServlet/resetSelectAdminPass',$('.aid:checked').serialize(),function(data){
  		  				if(data==1){
  		  					layer.msg('初始化成功',{
  		  						time:1000
  		  					});
  		  				}else{
  		  					layer.msg('初始化失败',{
  		  						time:1000
  		  					});
  		  				}
  		  			});
  				}
  			});
  		}
  	}
    //删除管理员
    function deleteAdmin(){
    	if($(':checked').length==0){
    		layer.msg('至少需要选择一条记录！！！',{icon:5});
    	}else if($(':checked').length>1){
    		layer.msg('一旦删除无法恢复<br>您确定删除吗?',{
        		time:4000,
        		btn: ['确定','取消'],
        		yes: function(index){
        			 $.get('${pageContext.request.contextPath}/BackstageRoleServlet/deleteSelectAdmin',$('.aid:checked').serialize(),function(data){
        				if(data==1){
        					$('.aid:checked').each(function(){
        						var n = $(this).parent('tr').index();
        						$('table').find('tr:eq('+n+')').remove();
        					});
        					layer.msg('删除成功',{
        						time:1000
        					});
        				}else{
        					layer.msg('删除失败',{
        						time:1000
        					});
        				}
        			}); 
        		}
        	});
    	}else if($(':checked').length==1){
    		layer.msg('一旦删除无法恢复<br>您确定删除吗?',{
        		time:4000,
        		btn: ['确定','取消'],
        		yes: function(index){
        			$.get('${pageContext.request.contextPath}/BackstageRoleServlet/deleteAdmin',{"admin_id":$('.aid:checked').attr('admin_id')},function(data){
        				if(data==1){
        					$('.aid:checked').parent().parent().parent().remove();
        					layer.msg('删除成功',{
        						time:1000
        					});
        				}else{
        					layer.msg('删除失败',{
        						time:1000
        					});
        				}
        			});
        		}
        	});
    	}
    	
    }
    //修改管理员信息界面
    function editAdmin(){
   	  if($(':checked').length==0){
   	    	layer.msg('请选择一条记录！！！',{icon:5});
   	    }else if($(':checked').length>1){
   	    	layer.msg('只能选择一条记录！！！',{icon:5});
   	    }else{
   	    	layer.open({
   	    		type: 2,
   	            skin: 'body .demo-class .layui-layer-title{background:#c00; color:#fff; border: none;}',
   	            title:'管理员信息修改',
   	            area:['900px','330px'],
   	            closeBtn: 0,
   	            shadeClose:true,
   	            content:['${pageContext.request.contextPath}/pages/houtai/role/role_edit.jsp?admin_id='+$('.aid:checked').attr("admin_id"),'no']
   	             ,end: function () {
   	            	 if($('#panduan').val()==1){
   	            		 layer.msg('修改成功',{
   	   	                	time:1000
   	   	                });
   	            		$('#panduan').val(10);
   	            		$.get('${pageContext.request.contextPath}/BackstageRoleServlet/preEditAdmin?admin_id='+$('.aid:checked').attr("admin_id"),function(data){
   	            			var admin = JSON.parse(data);
   	            			$("tr[admin_id="+admin[0].admin_id+"]").find('td:eq(1)').text(admin[0].admin_account);
   	            			$("tr[admin_id="+admin[0].admin_id+"]").find('td:eq(2)').text(admin[0].role_name);
   	            			$("tr[admin_id="+admin[0].admin_id+"]").find('td:eq(3)').text(admin[0].admin_name);
   	            			$("tr[admin_id="+admin[0].admin_id+"]").find('td:eq(4)').text(admin[0].admin_create);
   	            			$("tr[admin_id="+admin[0].admin_id+"]").find('td:eq(6)').text(admin[1]);
   	            		});
   	            	 }else if($('#panduan').val()==0){
   	            		 layer.msg('修改失败',{
    	   	                	time:1000
    	   	                });
   	            		$('#panduan').val(10);
   	            	 }
   	               
   	            } 
   	    	});
   	    }
    }
    
    
    //新增管理员弹窗
    function addAdmin(){
    	layer.open({
    		type: 2,
            skin: 'body .demo-class .layui-layer-title{background:#c00; color:#fff; border: none;}',
            title:'管理员新增',
            area:['900px','280px'],
            closeBtn: 0,
            shadeClose:true,
            content:['${pageContext.request.contextPath}/pages/houtai/role/role_add.jsp','no']
    	 	,end: function () {
           	 if($('#panduan').val()==1){
           		 layer.msg('新增成功',{
  	                time:1000
  	              });
           		$('#panduan').val(10);
           	 }else if($('#panduan').val()==2){
           		 layer.msg('新增失败,该管理员已存在！',{
     	             time:1000
     	         });
           		$('#panduan').val(10);
           	 }else if($('#panduan').val()==0){
           		 layer.msg('新增失败',{
     	               time:1000
     	             });
           		$('#panduan').val(10);
             	 }
           	 
              
           } 
    	});
    }
    
    //启用与禁用效果
    function transitionState(obj){
    	if($(obj).val()=='启用'){
    		layer.msg('是否禁用', {
			  time: 20000, //20s后自动关闭
			  btn: ['是', '否'],
			  yes:function(index,layero){
				  $.get('${pageContext.request.contextPath}/BackstageRoleServlet/editAdminState',{"state_id":$(obj).attr("state_id"),"admin_id":$(obj).attr("admin_id")},function(data){
					  if(data==1){
						$(obj).removeClass('btn-info').addClass('btn-danger');
			    		$(obj).val('禁用');
			    		layer.msg('禁用成功',{
		    				time:1000
		    			});
					  }else{
		    			layer.msg('禁用失败',{
		    				time:1000
		    			});
					 }
				  });
			  	
			  }
			});
    	}else{
    		layer.msg('是否启用', {
			  time: 20000, //20s后自动关闭
			  btn: ['是', '否'],
			  yes:function(index,layero){
				  $.get('${pageContext.request.contextPath}/BackstageRoleServlet/editAdminState',{"state_id":$(obj).attr("state_id"),"admin_id":$(obj).attr("admin_id")},function(data){
					  if(data==1){
						  	$(obj).removeClass('btn-danger').addClass('btn-info');
			    			$(obj).val('启用');
			    			layer.msg('启用成功',{
			    				time:1000
			    			});
					  }else{
		    			layer.msg('启用失败',{
		    				time:1000
		    			});
				  }
				  });
			  }
			});
    		
    	}
    }	  //分页
    pagination(${pageCount},${pageNum});
</script>

</body>

</html>
